﻿using System;
using System.Collections.Generic;

namespace KMLHelper
{
    public class KMLLine : KMLGeometry
    {
        private bool tessellate = true;
        /// <summary>
        /// 设置是否允许线条根据地形而自动调整；
        /// 只适用于LineString
        /// </summary>
        public bool Tessellate
        {
            set { this.tessellate = value; }
        }

        private string begin = null;
        /// <summary>
        /// 轨迹的开始时间
        /// </summary>
        public string TimeBeginStr
        {
            set
            {
                DateTime time = DateTime.Now;
                if (DateTime.TryParse(value, out time))
                {
                    this.begin = time.ToString(KMLConstant.TimeFormat);
                }
            }
        }
        /// <summary>
        /// 轨迹的开始时间
        /// </summary>
        public DateTime TimeBegin
        {
            set
            {
                this.begin = value.ToString(KMLConstant.TimeFormat);
            }
        }

        private string end = null;
        /// <summary>
        /// 轨迹的结束时间
        /// </summary>
        public string TimeEndStr
        {
            set
            {
                DateTime time = DateTime.Now;
                if (DateTime.TryParse(value, out time))
                {
                    this.end = time.ToString(KMLConstant.TimeFormat);
                }
            }
        }
        /// <summary>
        /// 轨迹的结束时间
        /// </summary>
        public DateTime TimeEnd
        {
            set
            {
                this.end = value.ToString(KMLConstant.TimeFormat);
            }
        }

        private List<KMLVector3> coords = null;
        /// <summary>
        /// 坐标集合
        /// </summary>
        public List<KMLVector3> Coords
        {
            set { this.coords = value; }
            get { return this.coords; }
        }

        public KMLLine()
        {
            this.geoType = EnumKMLGeoType.Line;
        }

        public override void Write(System.IO.StreamWriter sw)
        {
            try
            {
                if (this.coords != null && this.coords.Count > 0)
                { 
                    if (string.IsNullOrEmpty(this.begin) == false || 
                        string.IsNullOrEmpty(this.end) == false)
                    {
                        sw.WriteLine("<TimePeriod>");
                        if (string.IsNullOrEmpty(this.begin) == false)
                        {
                            sw.WriteLine("<begin>");
                            sw.WriteLine("<TimeInstant>");
                            sw.WriteLine("<timePosition>" + begin + "</timePosition>");
                            sw.WriteLine("</TimeInstant>");
                            sw.WriteLine("</begin>");
                        }
                        if (string.IsNullOrEmpty(this.end) == false)
                        {
                            sw.WriteLine("<end>");
                            sw.WriteLine("<TimeInstant>");
                            sw.WriteLine("<timePosition>" + end + "</timePosition>");
                            sw.WriteLine("</TimeInstant>");
                            sw.WriteLine("</end>");
                        }
                        sw.WriteLine("</TimePeriod>");
                    }

                    sw.WriteLine("<LineString>");
                    sw.WriteLine("<tessellate>" + (this.tessellate ? "1" : "0") + "</tessellate>");
                    sw.WriteLine("<coordinates>");
                    foreach (KMLVector3 pt in this.coords)
                    {
                        sw.Write(pt.Longitude + "," + pt.Latitude + "," + pt.Elevation + " ");
                    }
                    sw.WriteLine();
                    sw.WriteLine("</coordinates>");
                    sw.WriteLine("</LineString>");
                }
            }
            catch
            { throw; }
        }
    }
}
